<?php
// Module Body

function courier_user_add(&$data) {
	global $cfg;

        $unixname=get_property('unix user', $data['id'], $data['username']);
        $domain=fetchdata("host,domain","domain",$data['id']);
        $unixuid=exec('grep "'.$unixname.':" /etc/passwd|cut -d ":" -f3');
        $unixgid=exec('grep "g'.$data['id'].'" /etc/group| cut -d ":" -f3');
        //var_dump($data);
        //var_dump($domain);
        $pw2userdb=explode("|",exec($cfg['prog']['pw2userdb']."|grep -ir users|grep uid=".$unixuid));
        //var_dump($pw2userdb);
        foreach($pw2userdb as $pwkey => $pwval){
            if(stristr($pwval,"systempw=")){
                $passwd=$pwval;
                $pwval=explode("=",$pwval);
                $passwd=$pwval[1];
                echo $passwd;
            }
        }
        exec("echo -e '".$data['username']."@".$domain['domain']."\tsystempw=".$passwd."|uid=".$unixuid."|home=".$data['root']."|gid=".$unixgid."' >> /etc/courier/userdb/".$domain['domain']);
        exec($cfg['prog']['makeuserdb']);
}

function courier_user_update(&$data) {
	global $cfg;

        unix_username($data);
        $unixname=$data['username'];
        $data['username']=$data['username1'];
        $domain=fetchdata("host,domain","domain",$data['id']);
        $unixuid=exec('grep "'.$unixname.':" /etc/passwd|cut -d ":" -f3');
        $unixgid=exec('grep "g'.$data['id'].'" /etc/group| cut -d ":" -f3');

        $pw2userdb=explode("|",exec($cfg['prog']['pw2userdb']."|grep -ir uid|grep uid=".$unixuid));
        //var_dump($pw2userdb);
        foreach($pw2userdb as $pwkey => $pwval){
            if(stristr($pwval,"systempw=")){
                $passwd=$pwval;
                $pwval=explode("=",$pwval);
                $passwd=$pwval[1];
            }
        }

        $filearray = file ("/etc/courier/userdb/".$domain['domain']);
        $newarray = array();
        
        foreach ($filearray as $dkey => $duser ){
            if(stristr( $duser, "uid=".$unixuid)){
                $line=explode("systempw=",$duser);
                $newline=$line[0]."systempw=".$passwd;
                $line2=explode("|uid=",$line[1]);
                $newline.="|uid=".$line2[1];
                $newarray[] = $newline;
            }else $newarray[] = $duser;
        }
        
        $fp=fopen("/etc/courier/userdb/".$domain['domain'],"w");
        fwrite($fp,implode("",$newarray));
        exec($cfg['prog']['makeuserdb']);
}

function courier_monitor(&$data) {
	global $cfg;
	if (file_exists($cfg['spid']['pop3'])) {
		$pid = intval(trim(file_get_contents($cfg['spid']['pop3'])));
		if (posix_kill($pid,0))
			$data['pop3'] = 'up';
		else {
			$data['pop3'] = 'down';
		}
	} else {
		$data['pop3'] = 'down';	
	}
	if (file_exists($cfg['spid']['pop3-ssl'])) {
		$pid = intval(trim(file_get_contents($cfg['spid']['pop3-ssl'])));
		if (posix_kill($pid,0))
			$data['pop3-ssl'] = 'up';
		else {
			$data['pop3-ssl'] = 'down';
		}
	} else {
		$data['pop3-ssl'] = 'down';	
	}
	if (file_exists($cfg['spid']['imap'])) {
		$pid = intval(trim(file_get_contents($cfg['spid']['imap'])));
		if (posix_kill($pid,0))
			$data['imap'] = 'up';
		else {
			$data['imap'] = 'down';
		}
	} else {
		$data['imap'] = 'down';	
	}
	if (file_exists($cfg['spid']['imap-ssl'])) {
		$pid = intval(trim(file_get_contents($cfg['spid']['imap-ssl'])));
		if (posix_kill($pid,0))
			$data['imap-ssl'] = 'up';
		else {
			$data['imap-ssl'] = 'down';
		}
	} else {
		$data['imap-ssl'] = 'down';	
	}
	if (file_exists($cfg['spid']['authdaemon'])) {
		$pid = intval(trim(file_get_contents($cfg['spid']['authdaemon'])));
		if (posix_kill($pid,0))
			$data['authdaemon'] = 'up';
		else {
			$data['authdaemon'] = 'down';
		}
	} else {
		$data['authdaemon'] = 'down';	
	}
	if ($data['pop3'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['pop3']) {
			webcp_log(2,0,"monitor","service down: pop3 - attempting to start",0,$echo);
			courier_start("pop3");
			sleep(2);
			if (file_exists($cfg['spid']['pop3'])) {
				$pid = intval(trim(file_get_contents($cfg['spid']['pop3'])));
				if (posix_kill($pid,0))
					$data['pop3'] = 'up';
				else {
					$data['pop3'] = 'down';
				}
			} else {
				$data['pop3'] = 'down';	
			}
		}
	}
	if ($data['pop3-ssl'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['pop3-ssl']) {
			webcp_log(2,0,"monitor","service down: pop3-ssl - attempting to start",0,$echo);
			courier_start("pop3-ssl");
			sleep(2);
			if (file_exists($cfg['spid']['pop3-ssl'])) {
				$pid = intval(trim(file_get_contents($cfg['spid']['pop3-ssl'])));
				if (posix_kill($pid,0))
					$data['pop3-ssl'] = 'up';
				else {
					$data['pop3-ssl'] = 'down';
				}
			} else {
				$data['pop3-ssl'] = 'down';	
			}
		}
	}
	if ($data['imap'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['imap']) {
			webcp_log(2,0,"monitor","service down: imap - attempting to start",0,$echo);
			courier_start("imap");
			sleep(2);
			if (file_exists($cfg['spid']['imap'])) {
				$pid = intval(trim(file_get_contents($cfg['spid']['imap'])));
				if (posix_kill($pid,0))
					$data['imap'] = 'up';
				else {
					$data['imap'] = 'down';
				}
			} else {
				$data['imap'] = 'down';	
			}
		}
	}
	if ($data['imap-ssl'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['imap-ssl']) {
			webcp_log(2,0,"monitor","service down: imap-ssl - attempting to start",0,$echo);
			courier_start("imap-ssl");
			sleep(2);
			if (file_exists($cfg['spid']['imap-ssl'])) {
				$pid = intval(trim(file_get_contents($cfg['spid']['imap-ssl'])));
				if (posix_kill($pid,0))
					$data['imap-ssl'] = 'up';
				else {
					$data['imap-ssl'] = 'down';
				}
			} else {
				$data['imap-ssl'] = 'down';	
			}
		}
	}
	if ($data['authdaemon'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['authdaemon']) {
			webcp_log(2,0,"monitor","service down: courier authdaemon - attempting to start",0,$echo);
			courier_start("authdaemon");
			sleep(2);
			if (file_exists($cfg['spid']['authdaemon'])) {
				$pid = intval(trim(file_get_contents($cfg['spid']['authdaemon'])));
				if (posix_kill($pid,0))
					$data['authdaemon'] = 'up';
				else {
					$data['authdaemon'] = 'down';
				}
			} else {
				$data['authdaemon'] = 'down';	
			}
		}
	}
	if ($data['pop3'] == 'down') {
		webcp_log(1,0,"monitor","service down: pop3",0,$echo);
	}
	if ($data['pop3-ssl'] == 'down') {
		webcp_log(1,0,"monitor","service down: pop3-ssl",0,$echo);
	}
	if ($data['imap'] == 'down') {
		webcp_log(1,0,"monitor","service down: imap",0,$echo);
	}
	if ($data['imap-ssl'] == 'down') {
		webcp_log(1,0,"monitor","service down: imap-ssl",0,$echo);
	}
	if ($data['authdaemon'] == 'down') {
		webcp_log(1,0,"monitor","service down: courier authdaemon",0,$echo);
	}
	return true;
}

// Add cfg vars that relate to pop3
function courier_add_cfg(&$cfg) {
	if ($cfg['spid']['pop3'] == '/var/run/inetd.pid' && $cfg['init']['pop3'] == '/etc/init.d/inetd') {
		$cfg['spid']['pop3']	= '/var/run/inetd.pid';
		$cfg['init']['pop3']	= '/etc/init.d/inetd';
	}
	$cfg['autostart']['pop3'] = false;
}

// Start the pop3 server
function courier_start($service) {
	global $cfg;
	exec($cfg['init'][$service].' start');
}

// Stop the pop3 server
function courier_stop($service) {
	global $cfg;
	exec($cfg['init'][$service].' stop');
}

function courier_restart($service) {
	global $cfg;
	exec($cfg['init'][$service].' restart');
}
?>